package com.yegs.admin.business.controler;


import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.yegs.admin.base.PageVo;
import com.yegs.admin.business.entity.BaseRole;
import com.yegs.admin.business.serviceImpl.BaseMenuServiceImpl;
import com.yegs.admin.business.serviceImpl.BaseRoleServiceImpl;
import com.yegs.admin.config.Msg;
import org.apache.shiro.authz.annotation.RequiresPermissions;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.HashMap;
import java.util.List;

/**
 * <p>
 *  前端控制器
 * </p>
 *
 * @author yegs
 * @since 2020-08-20
 */
@RestController
@RequestMapping("/baseRole")
public class BaseRoleController{

    @Autowired
    private BaseRoleServiceImpl baseRoleService;

    @Autowired
    private BaseMenuServiceImpl baseMenuService;

    @GetMapping("/delbyid")
    @RequiresPermissions("baseRole:delbyid")
    public Msg delById(@RequestParam("id") Long id){
        try {
            baseMenuService.removeById(id);
            return Msg.success("删除成功");
        }catch (Exception e){
            e.printStackTrace();
            return Msg.error(e.getMessage());
        }
    }
    //保存||更新
    @PostMapping("/save")
    @RequiresPermissions("baseRole:save")
    public Msg save(@RequestBody  BaseRole t){
        try {
            baseRoleService.saveRoleHaveMenuids(t);
            return Msg.success("保存成功");
        } catch (Exception e) {
            e.printStackTrace();
            return Msg.error("内部错误!");
        }
    }


    //查询
    @PostMapping("/query")
    @RequiresPermissions("baseRole:query")
    public Msg query(@RequestBody PageVo<BaseRole> pageVo){
        try {
            Page page = new Page(pageVo.getPageNumber(),pageVo.getPageSize());
            QueryWrapper<BaseRole> queryWrapper = new QueryWrapper<BaseRole>();
            queryWrapper.setEntity(pageVo.getEntity());
            page = baseRoleService.page(page,queryWrapper);
            return Msg.success("page", page);
        }catch (Exception e){
            e.printStackTrace();
            return Msg.error(e.getMessage());
        }
    }



    @PostMapping("/saveMenus")
    @RequiresPermissions("baseRole:saveMenus")
    public Msg saveMenus(@RequestBody BaseRole baseRole){
        try {
            baseMenuService.saveMenus(baseRole);
            return Msg.success("保存成功");
        } catch (Exception e) {
            e.printStackTrace();
            return Msg.error(e.getMessage());
        }
    }

    /**
     * 分页查询角色
     * @param t
     * @return
     */
    @PostMapping("/getRoles")
    @RequiresPermissions("baseRole:getRoles")
    public Msg getRoles(BaseRole t){
        Page page = new Page();
        QueryWrapper<BaseRole> queryWrapper = new QueryWrapper();
        queryWrapper.setEntity(t);
        page = baseRoleService.page(page,queryWrapper);
        HashMap<String,Object> hashMap = new HashMap<>();
        List records = page.getRecords();
        hashMap.put("list",page.getRecords());
        hashMap.put("total",page.getTotal());
        return Msg.success(hashMap);
    }
    @GetMapping("/getAllRoles")
    @RequiresPermissions("baseRole:getAllRoles")
    public Msg getAllRoles(){
        List<BaseRole> list = baseRoleService.list();
        HashMap hashMap = new HashMap();
        hashMap.put("list",list);
        return Msg.success(hashMap);
    }


    @GetMapping("/findRoleHaveMenu")
    @RequiresPermissions("baseRole:findRoleHaveMenu")
    public Msg findRoleHaveMenu(){
        List<BaseRole> roleHaveMenu = baseRoleService.findRoleHaveMenu();
        HashMap hashMap = new HashMap();
        hashMap.put("list",roleHaveMenu);
        return Msg.success(hashMap);
    }

    @PostMapping("/findRoleHaveMenuList")
    public Msg findRoleHaveMenuList(){
        List<BaseRole> roleHaveMenu = baseRoleService.findRoleHaveMenuList();
        HashMap hashMap = new HashMap();
        hashMap.put("list",roleHaveMenu);
        hashMap.put("total",roleHaveMenu.size());
        return Msg.success(hashMap);
    }
    @GetMapping("/findRolesHaveMenuId")
    public Msg findRolesHaveMenuId(){
        List<BaseRole> roleHaveMenu = baseRoleService.findRoleHaveMenuList();
        HashMap hashMap = new HashMap();
        hashMap.put("list",roleHaveMenu);
        hashMap.put("total",roleHaveMenu.size());
        return Msg.success(hashMap);
    }
}
